import {
  Controller,
  Get,
  Post,
  Put,
  Body,
  Query,
  UseGuards,
  Headers,
  Delete,
  Param,
} from '@nestjs/common';
import {
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
  ApiParam,
  ApiQuery,
  ApiTags,
} from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';

import { BaseResponse } from '@server/basic-dto';

import UserService from './service';

import { CreateUserOrUserLoginDto, UserLoginResponse } from './dto';

@ApiTags('user')
@Controller('/user')
export default class UserController {
  constructor(private readonly service: UserService) {}

  @ApiOperation({
    summary: '创建用户',
  })
  @ApiResponse({
    type: BaseResponse,
  })
  @Post('/create')
  async create(
    @Body() createUser: CreateUserOrUserLoginDto,
  ): Promise<BaseResponse> {
    return this.service.create(createUser);
  }

  @ApiOperation({
    summary: '用户登录',
  })
  @ApiResponse({
    type: UserLoginResponse,
  })
  @Post('/login')
  async login(
    @Body() loginUser: CreateUserOrUserLoginDto,
  ): Promise<UserLoginResponse> {
    return this.service.login(loginUser);
  }

  @UseGuards(AuthGuard())
  @ApiOperation({
    summary: '退出登录',
  })
  @ApiBearerAuth()
  @Get('/logout')
  async logout(@Headers('authorization') token: string): Promise<BaseResponse> {
    return this.service.logout(token);
  }
}
